package com.controller;

import com.entity.User;
import com.resp.BaseResponseEntity;
import com.service.ICartService;
import com.service.IPhoneService;
import com.service.IUserService;
import com.service.impl.CartServiceimpl;
import com.service.impl.PhoneServiceimpl;
import com.service.impl.UserServiceimpl;
import com.utlils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 添加购物
 */
@WebServlet("/inserCart")
public class CreateController extends HttpServlet {
    ICartService service=new CartServiceimpl();
    IUserService userService=new UserServiceimpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户ID 和商品ID  创建对应的购物车
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //获取商品ID和用户ID
        String uname = req.getParameter("name");
        String parameter = req.getParameter("pid");
        //根据返回的用户名取用户ID
        User us = userService.findByName(uname);
        int uid = us.getId();
        int pid = Integer.parseInt(parameter);

        Integer integer = service.inserCart(uid, pid);

        BaseResponseEntity entity=null;

        if(integer<1){
            entity=new BaseResponseEntity(200,"加入成功",integer);
        }else {
            entity=BaseResponseEntity.AddCartBug();
        }

        ResponseUtils.responseToJson(resp,integer);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
